// 点击登录/注册，切换表单页面

function formEvent() {
    //绑定点击事件到li
    $('form').on('click', 'ul>li', function (e) {
        target = e.target || e.srcElement
        $(target).addClass('underscore').siblings().removeClass('underscore')
        $(target).addClass('block').siblings().removeClass('block')
        let index = $(target).attr('index')
        $(`form>.userData>div[index="${index}"]`).addClass('block').siblings().removeClass('block')
        $(`form>.userData>div[index="${index}"]`).removeClass('none').siblings().addClass('none')
    })
    //点击改变复选框状态
    $('form').on('click', 'input[type="checkbox"]', function () {
        if ($('form input[type="checkbox"]').attr('state') == 0) {
            $('form input[type="checkbox"]').attr('state', 1)
            $('form input[type="checkbox"]').prop('checked', true)

        } else {
            $('form input[type="checkbox"]').attr('state', 0)
            $('form input[type="checkbox"]').prop('checked', false)
        }
    })
    //密码用户名非空验证
    $('form>.userData input[name="username"],input[name="password"]').blur(function () {
        if ($(this).val()) {
            $(this).attr('state', 1)
        } else {
            alert('用户名密码不能为空')
            $(this).attr('state', 0)
        }
    })
    //登录、注册表单验证
    $('form>.userData button').on('click', function (e) {
        e.preventDefault()
        if ($(this).text() == '登录') {
            let stateArr = [...$(this).siblings()]
            stateArr = stateArr.filter(item => $(item).prop('type') ? item : '')
            let state = stateArr.every(item => $(item).attr('state') == true)
            if (state) {
                let inpEleN = stateArr.find(item => $(item).prop('name') == 'username')
                let name = $(inpEleN).val()
                let inpEleP = stateArr.find(item => $(item).prop('name') == 'password')
                let password = $(inpEleP).val()
                getData(name, password)
                async function getData(username, password) {
                    let data = await myPromise({
                        method: 'post',
                        url: 'http://api.yuguoxy.com/api/member/login',
                        data: {
                            username,
                            password
                        }
                    })
                    let { resultCode, resultInfo } = data
                    if (resultCode == 1) {
                         alert('登录成功')
                         location.href='./index.html'
                    }
                    if(resultCode == -1){
                        alert(resultInfo)
                    }
                }

            }

        }
        if ($(this).text() == '注册') {
            let stateArr = [...$(this).siblings()]
            stateArr = stateArr.filter(item => $(item).prop('type') ? item : '')
            let state = stateArr.every(item => $(item).attr('state') == true)
            if (state) {
                let inpEleN = stateArr.find(item => $(item).prop('name') == 'username')
                let name = $(inpEleN).val()
                let inpEleP = stateArr.find(item => $(item).prop('name') == 'password')
                let password = $(inpEleP).val()
                getData(name, password)
                async function getData(username, password) {
                    let data = await myPromise({
                        method: 'post',
                        url: 'http://api.yuguoxy.com/api/member/add',
                        data: {
                            username,
                            password
                        }
                    })
                    let { resultCode, resultInfo } = data
                    if (resultCode == 1) {
                        alert('注册成功')
                        location.reload()
                    }
                }

            }

        }
    })
}
formEvent()
